<?php

class ProductController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

	public function enquiryAction()
    {
        $form = new Application_Form_Product();
    	$form->submit->setLabel('Search');
    	$this->view->form = $form;
    	
    	if ($this->getRequest()->isPost()) {
    		$formData = $this->getRequest()->getPost();
    		if ($form->isValid($formData)) {
    			
    			$prod_no = (int)$form->getValue('prod_no');
    			$prod_name = $form->getValue('prod_name');
    			$prod_type = $form->getValue('prod_type');
    			$prod_description = $form->getValue('prod_description');
    			$prod_min_price = $form->getValue('prod_min_price');
    			$prod_max_price = $form->getValue('prod_max_price');
    			
    			if ($prod_no != null){
    				$params['prod_no'] = $prod_no;
    			}
    			if ($prod_name != null){
    				$params['prod_name'] = $prod_name;
    			}
    			if ($prod_type != null) {
    				$params['prod_type'] = $prod_type;
    			}
    			if ($prod_description != null) {
    				$params['prod_description'] = $prod_description;
    			}
    			if ($prod_min_price != null) {
    				$params['prod_min_price'] = $prod_min_price;
    			}
    			if ($prod_max_price != null) {
    				$params['prod_max_price'] = $prod_max_price;
    			}
    			
    			if (isset($params)) {
    				$this->_helper->redirector('enquiry', 'product', null, $params);
    			} else {
    				$this->_helper->redirector('enquiry', 'product');
    			}
    			 
    		} else {
    			$form->populate($formData);
    		}
    	} 
    	else {
    		/*
    		$request = $this->getRequest();
    		
    		$search = null;
    		
    		$cust_no = (int)$request->getParam('cust_no');
    		if ($cust_no != 0){
    			$form->cust_no->setValue($cust_no);
    			$search = $this->search('cust_no', $cust_no, $search);
    		}
    		 */
    		
    		
    		$request = $this->getRequest();
    		
    		$search = null;
    		$searchAlt = null;
    		
    		$altAsk = $request->getParam('alt');
    		
    		$prod_no = (int)$request->getParam('prod_no');
    		if ($prod_no != 0){
    			$form->prod_no->setValue($prod_no);
    			$search = $this->search('prod_no', $prod_no, $search);
    		}
    		
    		$prod_name = $request->getParam('prod_name');
    		if ($prod_name != null) {
    			$form->prod_name->setValue($prod_name);
    			$search = $this->search('prod_name', $prod_name, $search);
    		}
    		
    		$prod_type = $request->getParam('prod_type');
    		if ($prod_type != null) {
    			$form->prod_type->setValue($prod_type);
    			$search = $this->search('prod_type', $prod_type, $search);
    			
    		}
    		
    		
    		$prod_description = $request->getParam('prod_description');
    		if ($prod_description != null) {
    			$form->prod_description->setValue($prod_description);
    			$search = $this->search('prod_description', $prod_description, $search);
    		}
    		
    		$prod_min_price = $request->getParam('prod_min_price');
    		$prod_max_price = $request->getParam('prod_max_price');
    		
    		
    		
    		if ($prod_min_price != null && $prod_max_price != null) {
    			$form->prod_min_price->setValue($prod_min_price);
    			$form->prod_max_price->setValue($prod_max_price);
    			if ($search != null) {
    				$search = $search . ' AND (prod_price >= ' . $prod_min_price . ' AND prod_price <= ' . $prod_max_price . ')';
    			} else {
    				$search = '(prod_price >= ' . $prod_min_price . ' AND prod_price <= ' . $prod_max_price . ')';
    			}
    		} elseif ($prod_min_price != null && $prod_max_price == null) {
    			$form->prod_min_price->setValue($prod_min_price);
    			if ($search != null) {
    				$search = $search . ' AND prod_price >= ' . $prod_min_price;
    			} else {
    				$search = 'prod_price >= ' . $prod_min_price;
    			}
    		} elseif ($prod_min_price == null && $prod_max_price != null) {
    			$form->prod_max_price->setValue($prod_max_price);
    			if ($search != null) {
    				$search = $search . ' AND prod_price <= ' . $prod_max_price;
    			} else {
    				$search = 'prod_price <= ' . $prod_max_price;
    			}
    		}
    		
    		
    
    		
    		
    		
    		
    		$db = new Zend_Db_Adapter_Pdo_Mysql(array(
    		    		'host'     => 'amun1.eng.uts.edu.au',
    		    	    'port'	   => '5006',
    		    		'username' => 'test',
    		    		'password' => '1234',
    		    		'dbname'   => 'tops'
    		));
    		$db ->setFetchMode(Zend_Db::FETCH_OBJ);
    		
    		if ($search != null) {
    			$selectProducts = $db->select()
    			->from(array('p'=>'products'), array('prod_no', 'prod_name', 'prod_type', 'prod_description', 'prod_price', 'prod_quantity', 'prod_leadtime', 'dep_no', 'supplier_no'))
    			->joinLeft(array('d'=>'departments'), 'p.dep_no = d.dep_no', array('dep_name'))
    			->where($search);
    			$this->view->products = $db->fetchAll($selectProducts);
    		} else {
    			$selectProducts = $db->select()
    				->from(array('p'=>'products'), array('prod_no', 'prod_name', 'prod_type', 'prod_description', 'prod_price', 'prod_quantity', 'prod_leadtime', 'dep_no', 'supplier_no'))
    				->joinLeft(array('d'=>'departments'), 'p.dep_no = d.dep_no', array('dep_name'));
    			$this->view->products = $db->fetchAll($selectProducts);
    		}
    		
    		
    		if ($altAsk == "yes")
    		{
    			$alt_dep_no = $request->getParam('dep_no');
    			$alt_prod_type = $prod_type;
    		
    		
    		
    			if($alt_prod_type != null)
    			{
    				$selectAlternatives = $db->select()
    					->from(array('p'=>'products'), array('prod_no', 'prod_name', 'prod_type', 'prod_description', 'prod_price', 'prod_sale_price', 'prod_quantity', 'prod_leadtime', 'dep_no', 'supplier_no'))
    					->join(array('d'=>'departments'), 'p.dep_no = d.dep_no', array('dep_name'))
    					->where('prod_type = ?', $alt_prod_type)
    			    	->where('prod_quantity > 1')
    					->where('d.dep_no = ?', $alt_dep_no);
    				//dep_no
    				$this->view->alternatives = $db->fetchAll($selectAlternatives);
    				$this->view->index = $prod_no;
    				$this->view->altGo = $altAsk;
    			}
    		}
    		
    		/*if ($searchAlt != null) {
    			$selectAlternatives = $db->select()
    			->from(array('p'=>'products'), array('prod_no', 'prod_name', 'prod_type', 'prod_description', 'prod_price', 'prod_quantity', 'prod_leadtime', 'dep_no', 'supplier_no'))
    			->joinLeft(array('d'=>'departments'), 'p.dep_no = d.dep_no', array('dep_name'))
    			->where($searchAlt);
    			$this->view->alternatives = $db->fetchAll($selectAlternatives);
    		}*/
    		
    		/*
    		if ($prod_min_price == null && $prod_max_price == null) {
    			$selectProducts = $db->select()
				->from(array('p'=>'products'), 
						array('prod_no', 'prod_name', 'prod_type', 'prod_description', 'prod_price', 'prod_quantity', 'prod_leadtime', 'dep_no', 'supplier_no')    			
    				 )
    			->join(array('d'=>'departments'), ('p.dep_no = d.dep_no'), array('dep_name')
    				)
    			->where('prod_no = ?', $prod_no)
    			->orWhere('prod_name Like ?', '%'.$prod_name.'%')
    			->orWhere('prod_type = ?', $prod_type)
    			->orWhere('prod_description Like ?', '%'.$prod_description.'%');
    		} elseif ($prod_min_price != null && $prod_max_price == null) {
    			
    			$selectProducts = $db->select()
				->from(array('p'=>'products'), 
						array('prod_no', 'prod_name', 'prod_type', 'prod_description', 'prod_price', 'prod_quantity', 'prod_leadtime', 'dep_no', 'supplier_no')    			
    				 )
    			->join(array('d'=>'departments'), ('p.dep_no = d.dep_no'), array('dep_name')
    				)
    			->orWhere('prod_name Like ?', '%'.$prod_name.'%')
    			->orWhere('prod_type = ?', $prod_type)
    			->orWhere('prod_description Like ?', '%'.$prod_description.'%')
    			->orWhere('prod_price >= ?', $prod_min_price);
    		} elseif ($prod_min_price == null && $prod_max_price != null) {
    			$selectProducts = $db->select()
				->from(array('p'=>'products'), 
						array('prod_no', 'prod_name', 'prod_type', 'prod_description', 'prod_price', 'prod_quantity', 'prod_leadtime', 'dep_no', 'supplier_no')    			
    				 )
    			->join(array('d'=>'departments'), ('p.dep_no = d.dep_no'), array('dep_name')
    				)
    			->where('prod_no = ?', $prod_no)
    			->orWhere('prod_name Like ?', '%'.$prod_name.'%')
    			->orWhere('prod_type = ?', $prod_type)
    			->orWhere('prod_description Like ?', '%'.$prod_description.'%')
    			->orWhere('prod_price <= ?', $prod_max_price);
    		} else {
    			$selectProducts = $db->select()
				->from(array('p'=>'products'), 
						array('prod_no', 'prod_name', 'prod_type', 'prod_description', 'prod_price', 'prod_quantity', 'prod_leadtime', 'dep_no', 'supplier_no')    			
    				 )
    			->join(array('d'=>'departments'), ('p.dep_no = d.dep_no'), array('dep_name')
    				)
    			->where('prod_no = ?', $prod_no)
    			->orWhere('prod_name Like ?', '%'.$prod_name.'%')
    			->orWhere('prod_type = ?', $prod_type)
    			->orWhere('prod_description Like ?', '%'.$prod_description.'%')
    			->orWhere("prod_price >= $prod_min_price AND prod_price <= $prod_max_price");
    		
    		
    			}
    			$this->view->products = $db->fetchAll($selectProducts);
    			*/
    		
    	}
    }
	public function confirmAction()
	{
		
	}
    public function requestAction()
    {
    	$request = $this->getRequest();
    	$prod_no = $this->_getParam('prod_no');
    	 
    	$form = new Application_Form_Crequest();
    	$this->view->form = $form;
    	$form->prod_no->setValue($prod_no);
    	
    	if ($this->getRequest()->isPost()) {
    		$formData = $this->getRequest()->getPost();
    		if ($form->isValid($formData)) {
    	
    			$cust_no = $form->getValue('cust_no');
    			
    			$customer = new Application_Model_DbTable_Customers();
    			$customer->givecatCustomer($cust_no, $prod_no);
    			$this->_helper->redirector('confirm', 'product');
    		} else {
    			$form->populate($formData);
    		}
    	}
    	
    }
    
    public function search($name, $param, $search)
    {
    	if ($search != null) {
    		if (is_numeric($param)) {
    			$search = $search . ' AND ' . $name . ' Like "%' . $param . '%"';
    		} else {
    			$search = $search . ' AND ' . $name . ' Like "%' . $param . '%"';
    		}
    	} else {
    		if (is_numeric($param)) {
    			$search = $name . ' Like "%' . $param . '%"';
    		} else {
    			$search = $name . ' Like "%' . $param . '%"';
    		}
    	}
    	return $search;
    }
}
